package com.zxx.addressparse;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zxx on 2021/5/21
 */
public class AddressUtilss {
    private Map<String, Bean> province = new HashMap<>();
    private Context mContext;
    private String mAddress;

    public AddressUtilss(Context context) {
        mContext = context;
        init();
    }

    private void init() {
        String jsonData = new GetJsonDataUtil().getJson(mContext, "addresscodes.json");
        try {
            Type type = new TypeToken<HashMap<String, Bean>>() {
            }.getType();

            province = new Gson().fromJson(jsonData, type);

            //int i = 0;
            //for (Map.Entry<String, Bean> entry : province.entrySet()) {
            //    i++;
            //    Log.e("Tag-->", "key= " + entry.getKey() + " ,value= " + entry.getValue() + "<--" + i);
            //}

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //无序的遍历
    public String parseCode(String code) {
        if (province == null) {
            return "";
        }

        for (Map.Entry<String, Bean> entry : province.entrySet()) {
            Bean probean = entry.getValue();

            //Log.e("Tag-->", code + "<--");
            //Log.e("Tag-->", entry.getKey() + "<--");
            if (entry.getKey().equals(code)) {
                return probean.name;
            }

            Map<String, Bean> city = probean.children;
            for (Map.Entry<String, Bean> beanEntry : city.entrySet()) {
                Bean citybean = beanEntry.getValue();
                if (beanEntry.getKey().equals(code)) {
                    return citybean.name;
                }

                Map<String, Bean> area = citybean.children;
                //直辖市 与自治区 没有区，所以为空
                if (area != null) {
                    for (Map.Entry<String, Bean> areaEntry : area.entrySet()) {
                        Bean areabean = areaEntry.getValue();
                        if (areaEntry.getKey().equals(code)) {
                            return areabean.name;
                        }
                    }
                }
                //else {
                //    Log.e("tag-->", probean.name + " - " + citybean.name);
                //}
            }
        }
        return "无效key";
    }

    public String parseCode2(String code) {
        if (province == null) {
            return "";
        }

        traverse(code, province);
        //当再有无效key时，mAddress仍然显示上一次的值
        return mAddress;
    }

    //递归函数不能return，否则在内层循环中就return掉了
    private void traverse(String code, Map<String, Bean> map) {
        for (Map.Entry<String, Bean> entry : map.entrySet()) {
            Bean bean = entry.getValue();

            if (entry.getKey().equals(code)) {
                mAddress = bean.name;
            }

            Map<String, Bean> area = bean.children;

            //直辖市 与自治区 没有区，所以为空
            if (area != null) {
                traverse(code, area);
            }
        }
    }


    class Bean {
        String code;
        String name;
        Map<String, Bean> children;
    }
}
